// pages/goodsInfo/goodsInfo.js
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
import request from '../../utils/request'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        show: false,
        goodsInfo: {},
        goodsImg: '',
        isShowMask: true,
        showShare: false,
        options: [
          { name: '微信', icon: 'wechat', openType: 'share' },
          { name: '微博', icon: 'weibo' },
          { name: '复制链接', icon: 'link' },
          { name: '分享海报', icon: 'poster' },
          { name: '二维码', icon: 'qrcode' },
        ],

        // 状态栏高度
        statusBarHeight: wx.getStorageSync('statusBarHeight') + 'px',
        statusBarPaddingTop: wx.getStorageSync('statusBarHeight') + 15 + 'px',
        // 导航栏高度
        navigationBarHeight: wx.getStorageSync('navigationBarHeight') + 'px',
        // 胶囊按钮高度
        menuButtonHeight: wx.getStorageSync('menuButtonHeight') + 'px',
        // 导航栏和状态栏高度
        navigationBarAndStatusBarHeight:
            wx.getStorageSync('statusBarHeight') +
            wx.getStorageSync('navigationBarHeight') +
            'px'
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: async function ({ gid }) {

        let goodsInfo = await request('/goods/getSingle?goodsId=' + gid)
        this.setData({
            goodsInfo: goodsInfo.data,
            goodsImg: 'http://rc48dvuay.bkt.clouddn.com/' + goodsInfo.data.imgUrl
        })
        console.log(goodsInfo)

        const { statusBarHeight, platform } = wx.getSystemInfoSync()
        const { top, height } = wx.getMenuButtonBoundingClientRect()

        // 状态栏高度
        wx.setStorageSync('statusBarHeight', statusBarHeight)
        // 胶囊按钮高度 一般是32 如果获取不到就使用32
        wx.setStorageSync('menuButtonHeight', height ? height : 32)

        // 判断胶囊按钮信息是否成功获取
        if (top && top !== 0 && height && height !== 0) {
            const navigationBarHeight = (top - statusBarHeight) * 2 + height
            // 导航栏高度
            wx.setStorageSync('navigationBarHeight', navigationBarHeight)
        } else {
            wx.setStorageSync(
                'navigationBarHeight',
                platform === 'android' ? 48 : 40
            )
        }

        this.setData({
            isShowMask: false
        })

    },
    showPopup() {
        this.setData({ show: true });
    },
    onClose() {
        this.setData({ show: false });
    },
    backBtn() {
        wx.navigateBack({
            delta: 1,
        })
    },
    async addCartGoods() {
        let gid = this.data.goodsInfo.id;
        let userInfo = wx.getStorageSync('youxuan-user-userInfo')
        console.log(userInfo.id, gid)
        let result = await request("/cart/add/goods", {
            goodsId: gid,
            num: 1,
            userId: userInfo.id
        }, "POST")
        console.log(result)
        if (result.code == 200) {
            wx.showToast({
                title: '添加成功',
                icon: "none"
            })
        }
    },
    shareBtn(event) {
        this.setData({ showShare: true });
        console.log("店家了")
    },    
    
    onShareClose() {
    this.setData({ showShare: false });
    },

    onShareSelect(event) {
    Toast(event.detail.name);
    this.onClose();
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})